Module Global1
    Private Const MAX_GUIDLEN = 80

    'Private Type GUID
    Private data1 As Long
    Private data2 As Integer
    Private data3 As Integer
    Private data4(7) As Byte
    'End Type

    'Private Declare Function CoCreateGuid Lib "ole32.dll" (ByRef g As Guid) As Long
    'Private Declare Function StringFromGUID2 Lib "ole32.dll" (ByVal g As Long, ByVal lpsz As String, ByVal cchMax As Long) As Long
    Private Function GenerateGUID() As String
        Dim sGUID As String
        sGUID = System.Guid.NewGuid().ToString()
        GenerateGUID = sGUID
        'MessageBox.Show(sGUID)
    End Function
    Public Function ReformatCreditCardNumber(ByVal Org As String) As String
        Dim temp As String = ""
        Dim i As Integer
        Dim c As String

        For i = 1 To Len(Org)
            c = Mid(Org, i, 1)
            If Asc(c) >= 48 And Asc(c) <= 57 Then temp = temp & c
        Next i
        ReformatCreditCardNumber = temp
    End Function

    'Public Function SafeString(ByVal v As Object) As String
    '    If IsNothing(v) Then ''Or IsNull(v) Then
    '        SafeString = ""
    '    Else
    '        SafeString = Trim(v)
    '    End If
    'End Function

    Public Function Make2Digits(ByVal i As Long) As String
        Dim s As String
        s = CStr(i)
        If Len(s) < 2 Then
            Make2Digits = "0" & s
        Else
            If Len(s) > 2 Then
                Make2Digits = Right(s, 2)
            Else
                Make2Digits = CStr(s)
            End If
        End If
    End Function


    Public Function Encode(ByVal s As String) As String
        Dim temp As String
        temp = Replace(s, "<", "%3C")
        temp = Replace(temp, ">", "%3E")
        temp = Replace(temp, """", "%22")
        Encode = temp
    End Function

    Public Function Decode(ByVal s As String) As String
        Dim temp As String
        temp = Replace(s, "%3C", "<")
        temp = Replace(temp, "%3E", ">")
        temp = Replace(temp, "%22", """")
        Decode = temp
    End Function

    Public Function Clip(ByVal Value As Long, ByVal Min As Long, ByVal Max As Long) As Long
        If Value < Min Then
            Clip = Min
        Else
            If Value > Max Then
                Clip = Max
            Else
                Clip = Value
            End If
        End If
    End Function

    'Public Function NewGUID() As String
    'Dim g As Guid
    'Dim hr As Long
    'Dim s As String
    'Dim i As Integer
    'Dim strSQL As String = ""
    'Dim Dr As sqldatareader
    'strSQL = " SELECT Session_id, text From sys.dm_exec_connections "
    'strSQL += " CROSS APPLY sys.dm_exec_sql_text(most_recent_sql_handle) as ST"
    'Dr = objdt.DataReader(strSQL, strError)
    'hr = GenerateGUID()
    ''hr = CoCreateGuid(g)
    'If (hr <> 0) Then
    '    NewGUID = ""
    '    Err.Raise(vbObjectError + hr)
    '    Exit Function
    'End If
    's = String.Concat(MAX_GUIDLEN, 0) 'return contain 0 max length-how many 0
    ''hr = StringFromGUID2(g, s, MAX_GUIDLEN)

    ''-- hr is length of returned guid string (including NULL at the end)
    'If (hr = 0) Then
    '    NewGUID = ""
    '    Err.Raise(vbObjectError + hr)
    '    Exit Function
    'End If
    'hr = hr - 1
    ''-- Convert Wide characters to ascii
    'For i = 1 To hr * 2 Step 2
    '    Mid(s, (i + 1) / 2, 1) = Mid(s, i, 1)
    'Next i
    'NewGUID = Left(s, hr)
    'End Function

    Public Function YNToBool(ByVal Value As Object)
        If SafeString(Value) = "Y" Then
            YNToBool = True
        Else
            YNToBool = False
        End If
    End Function

    Public Function BoolToYN(ByVal b As Boolean)
        If b Then
            BoolToYN = "Y"
        Else
            BoolToYN = "N"
        End If
    End Function
    Public Function FixLength(ByVal Source, ByVal Length)
        Dim temp As String
        If Len(Source) > Length Then
            temp = Right(Source, Length)
        Else
            temp = Source
            Do While Len(temp) < Length
                temp = temp & " "
            Loop
        End If
        FixLength = temp
    End Function

    'Public Function CheckString(ByVal sStringName As String) As String

    '    'This function ensures strings have valid quotes around them for
    '    'insertion into a database.

    '    sStringName = Replace(sStringName, "'", "''")

    '    sStringName = "'" & sStringName & "'"
    '    CheckString = sStringName

    'End Function

End Module
